﻿Imports System.Text.RegularExpressions
Imports System.Xml
Imports System.Text
Imports System.Xml.Xsl
Imports System.IO
Imports System.Security.Cryptography
Imports System.Net

Public Class WeatherClass

    Private _key As String = "ptkAg6YU"

    Public Function GetWeatherForecast(ByVal weatherXMLUrl As String, ByVal postcode As String, ByVal days As Integer, Optional ByVal styleSheetUrl As String = "") As String
        weatherXMLUrl = Regex.Replace(weatherXMLUrl, "&pc=(\d+)&", "&pc=" & postcode & "&")
        weatherXMLUrl = Regex.Replace(weatherXMLUrl, "&days=(\d+)&", "&days=" & days & "&")

        weatherXMLUrl += "&k=" & GetKey(DateTime.Now)
        Dim resolver As New XmlUrlResolver()
        Dim sb As New StringBuilder()
        resolver.Credentials = CredentialCache.DefaultCredentials
        Dim stylesheet As New XmlTextReader(styleSheetUrl)
        stylesheet.XmlResolver = resolver
        Dim transform As New XslCompiledTransform()
        transform.Load(stylesheet)
        Dim textWriter As TextWriter = New StringWriter(sb)
        Dim reader As XmlReader = New XmlTextReader(weatherXMLUrl)
        While reader.Read
            If reader.NodeType = XmlNodeType.Element Then
                If reader.Name.ToUpper = "LOCATION" Then
                    Dim x As String = "x"
                End If

            End If

        End While

        'Using reader As XmlReader = New XmlTextReader(weatherXMLUrl)
        '    Try
        '        Try
        '            Dim results As New XmlTextWriter(textWriter)
        '            transform.Transform(reader, results)

        '            reader.Close()
        '        Catch generatedExceptionName As XmlException
        '            Throw New ApplicationException("Invalid data from weather web service.")
        '        End Try

        '    Catch generatedExceptionName As XmlException
        '        Throw New ApplicationException("Invalid data from weather web service.")
        '    End Try
        'End Using
        Return sb.ToString()
    End Function

    Private Function GetKey(ByVal [date] As DateTime) As String
        Dim key As String = GetCalc([date]).ToString()
        Return GetMd5Hash(String.Concat(key, _key))
    End Function

    Private Function GetCalc(ByVal [date] As DateTime) As Integer
        Return ([date].Day * 2) + ([date].Month * 300) + (Int32.Parse([date].Year.ToString().Substring(2, 2)) * 170000)
    End Function

    Private Function GetMd5Hash(ByVal input As String) As String
        ' Create a new instance of the MD5CryptoServiceProvider object.
        Dim md5Hasher As MD5 = MD5.Create()

        ' Convert the input string to a byte array and compute the hash.
        Dim data As Byte() = md5Hasher.ComputeHash(Encoding.[Default].GetBytes(input))

        ' Create a new Stringbuilder to collect the bytes
        ' and create a string.
        Dim sBuilder As New StringBuilder()

        ' Loop through each byte of the hashed data 
        ' and format each one as a hexadecimal string.
        For i As Integer = 0 To data.Length - 1
            sBuilder.Append(data(i).ToString("x2"))
        Next

        ' Return the hexadecimal string.
        Return sBuilder.ToString()
    End Function



End Class
